package com.xxh.market.controller;

import com.xxh.market.dao.BookDao;
import com.xxh.market.dao.BookTypeDao;
import com.xxh.market.dao.impl.BookDaoImpl;
import com.xxh.market.dao.impl.BookTypeDaoImpl;
import com.xxh.market.entity.Book;
import com.xxh.market.entity.Booktype;
import com.xxh.market.entity.User;
import com.xxh.market.service.OrderService;
import com.xxh.market.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//书籍相关请求
@WebServlet(name = "BookServlet",urlPatterns = "/book/*")
public class BookServlet extends HttpServlet {

   BookTypeDao bookTypeDao= new BookTypeDaoImpl();
   BookDao bookDao=new BookDaoImpl();
   List<Book> carbooks=new ArrayList<Book>();

   OrderService orderService=new OrderServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String pathInfo = request.getPathInfo();
        HttpSession session = request.getSession();
        //处理index请求
        if(pathInfo.equals("/index")){
            List<Booktype> types = bookTypeDao.getAllTypes();
            session.setAttribute("booktypes", types);//页面显示
            Integer firstTypeID = types.get(0).getId();
            List<Book> books = bookDao.getBooksByTypeId(firstTypeID);
            session.setAttribute("booklist",books);
            //数据放到session中然后返回主页
            response.sendRedirect(request.getContextPath()+"/index.jsp");
        }

        //处理getTypeByID的请求
        if(pathInfo.equals("/getType")){
            String id = request.getParameter("id");
            int typeid = Integer.parseInt(id);
            List<Book> books = bookDao.getBooksByTypeId(typeid);
            session.setAttribute("booklist",books);
            //数据放到session中然后请求书籍的页面
            response.sendRedirect(request.getContextPath()+"/books.jsp");
        }
        if(pathInfo.equals("/search")){
            String bname = request.getParameter("searchName");
            System.out.println(bname);
            List<Book> books = bookDao.getBooksByName(bname);
            //更新session中的booklist
            session.setAttribute("booklist",books);
            //数据放到session中然后请求书籍的页面
            response.sendRedirect(request.getContextPath()+"/books.jsp");
        }
        if(pathInfo.equals("/addCar")){
            Integer id = Integer.parseInt(request.getParameter("bid"));
            Book book = bookDao.getBookByID(id);
            carbooks.add(book);
            session.setAttribute("car",carbooks);
            response.sendRedirect(request.getContextPath()+"/car.jsp");
        }
        //下单请求
        if(pathInfo.equals("/pay")){
            //拿到用户ID
            User userDetail = (User)session.getAttribute("userDetail");
            Integer uid = userDetail.getId();

            //拿到总价格
            String tprice = request.getParameter("tprice");
            double v = Double.parseDouble(tprice);
            //拿到已选书籍ID列表
            /*怎么写？*/
            List idList=new ArrayList<Integer>();
            carbooks = (List<Book>)session.getAttribute("car");
            String[] ids = request.getParameterValues("bid");
            //拿到已选择产品的ID列表
            Arrays.stream(ids).forEach(id->{
                int bid = Integer.parseInt(id);
                idList.add(bid);
                //下单时更新session里的购物车信息
                for (int i=0;i<carbooks.size();i++){
                    if(bid==carbooks.get(i).getId()){
                        carbooks.remove(i);
                    }
                }
            });

            //刷新购物车的商品信息
            session.setAttribute("car",carbooks);
            orderService.addOrder(uid,idList,v);
            //跳转到订单页 显示用户的所有订单
            response.sendRedirect(request.getContextPath()+"/order/selectByUser");

        }

    }
}
